package com.heo.music.player.dto.query;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * created on 2024-05-11 10:52
 *
 * @author 星天无极
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "音乐查询")
public class MusicInfoQuery extends PageQuery {

    @ApiModelProperty("关键字")
    private String keyword;


    public <T> Specification<T> convertSpec() {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (keyword != null) {
                String value = "%" + keyword + "%";
                predicates.add(
                    builder.or(
                        builder.like(root.get("title"), value),
                        builder.like(root.get("artist"), value),
                        builder.like(root.get("album"), value)
                    )
                );
            }
            return builder.and(predicates.toArray(new Predicate[0]));
        };
    }
}
